/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rational.sheet.table.cell.view;

import java.awt.Color;
import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.plaf.BorderUIResource;
import org.rational.sheet.table.RationalTable;
import org.rational.sheet.table.RationalTableData;
import org.rational.sheet.table.cell.model.Cell;
import org.rational.sheet.table.cell.model.CellAddress;
import org.rational.sheet.table.cell.model.setting.CellView;

/**
 *
 * @author Overmind
 */
public class RationalTableCellEditor
    extends DefaultCellEditor
{    
    public RationalTableCellEditor()
    {
	super(new JTextField());
    }
    
    private JTextField getJTextField()
    {
	return (JTextField)super.editorComponent;
    }
    
    @Override
    public Component getTableCellEditorComponent(JTable table, Object object, 
        boolean isSelected, int row, int column)
    {
	RationalTable rationalTable = (RationalTable)table;
	RationalTableData tableData = rationalTable.getTableData();
	
	JTextField field = (JTextField)super.getTableCellEditorComponent(table,
		object, isSelected, row, row);
	
	Cell cell = tableData.getCellAt(new CellAddress(row, column));
        CellView view = cell.getView();
	
	field.setText(cell.getFormula());
	field.setFont(view.getFont());
	
	field.setForeground(view.getTextColor());
	field.setOpaque(true);
	
	Border border = new BorderUIResource.LineBorderUIResource(Color.black);
	    field.setBorder(border);
	    field.setBackground(view.getBackgroundColor());
	
	field.setHorizontalAlignment(view.getHorizontalAlignment().getCode());
	
        return field;
    }
    
    @Override
    public Object getCellEditorValue() 
    {
	return getJTextField().getText();
    } 
}